package org.dm.TestCase;

import com.alibaba.fastjson2.JSONObject;
import okhttp3.Response;
import org.dm.controller.HttpController;
import org.testng.Reporter;
import org.testng.annotations.Test;
import org.dm.TestData.TianQiCaseData;

import java.util.HashMap;
import java.util.Map;


public class TianQiApiTest {

    HttpController controller = HttpController.getInstance();
    private final String url = "https://v0.yiketianqi.com/api";

    private HashMap<String, String> getDefaultParams(HashMap<String, String> params) {
        HashMap<String, String> map = new HashMap<>();
        map.put("appid", "62513211");
        map.put("appsecret", "Zojho4yH");
        map.putAll(params);
        return map;

    }

    @Test(dataProvider="getCity" , dataProviderClass = TianQiCaseData.class)
    public void getTianQi(String city) throws Exception {
        HashMap<String, String> params = new HashMap<>();
        params.put("version", "v61");
        params.put("city", city);
        params = this.getDefaultParams(params);

        Response response = controller.doGet(url,params);
        JSONObject jsonBody = controller.getJsonBody(response);

        System.out.println("--------------------\n" + jsonBody);
        HashMap<String, Object> hashMap = controller.getHashMapBody(jsonBody);
        System.out.println("--------------------\n" + hashMap);
        Reporter.log("Response Body:");
        for (Map.Entry<String, Object> item : hashMap.entrySet()) {
            Reporter.log(item.getKey() + " : " + item.getValue());
        }

    }


}
